package com.shipwe.loans.ui.activity

import android.content.Context
import android.content.Intent
import android.view.View
import com.shipwe.loans.R
import com.shipwe.loans.base.app.ShipweStackManager
import com.shipwe.loans.pro.ShipweActivity
import com.shipwe.loans.ui.fragment.FragmentOrderPayPinFailed
import com.shipwe.loans.ui.fragment.FragmentOrderPayPinProcess
import com.shipwe.loans.ui.fragment.FragmentOrderPayPinSuccess
import com.shipwe.loans.ui.presenter.OrderPayPinStatusPresenter
import com.shipwe.loans.ui.view.IOrderPayPinStatusView
import com.shipwe.loans.utils.event.AppEventData
import com.shipwe.loans.utils.shared.ShipweSharedHelper
import com.shipwe.loans.utils.shared.ShipweSharedKey
import org.json.JSONObject

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/27 17:04
 * @Annotation：PIN 支付结果处理中
 */
class ActivityOrderPayPin: ShipweActivity(),IOrderPayPinStatusView {
    var presenter: OrderPayPinStatusPresenter? = null

    companion object {
        private const val INTENT_KEY_PRODUCT = "product"
        private const val INTENT_KEY_TOKEN = "token"
        private const val INTENT_KEY_ORDER_ID = "thirdOrderId"
        private const val INTENT_KEY_POINTS = "points"
        fun start(context: Context, pName: String, pToken: String,orderId: String,points: String = "") {
            val intent = Intent(context, ActivityOrderPayPin::class.java)
            intent.putExtra(INTENT_KEY_PRODUCT, pName)
            intent.putExtra(INTENT_KEY_TOKEN, pToken)
            intent.putExtra(INTENT_KEY_ORDER_ID, orderId)
            intent.putExtra(INTENT_KEY_POINTS,points)
            context.startActivity(intent)
        }
    }

    override fun getShipweRootView(): View? {
        return null
    }

    override fun initShipweView() {
    }

    override fun initShipweListener() {

    }

    override fun initShipweData() {
        presenter = OrderPayPinStatusPresenter(this)
        presenter?.saveOrderPayPinThirdOrderId(getOrderPayThirdOrderId())
        presenter?.loadOrderPaymentPinStatusAction()
    }

    override fun getOrderPayProductName(): String {
        if (intent.extras == null){
            return ""
        }
        return intent.extras?.getString(INTENT_KEY_PRODUCT).toString()
    }

    override fun getOrderPayProductToken(): String {
        if (intent.extras == null){
            return ""
        }
        return intent.extras?.getString(INTENT_KEY_TOKEN).toString()
    }

    /**
     * 三方订单id
     */
    private fun getOrderPayThirdOrderId(): String {
        if (intent.extras == null){
            return ""
        }
        return intent.extras?.getString(INTENT_KEY_ORDER_ID).toString()
    }

    /**
     * 使用积分
     */
    override fun getOrderPayPointsCount(): String {
        if (intent.extras == null){
            return ""
        }
        return intent.extras?.getString(INTENT_KEY_POINTS).toString()
    }

    /**
     * 支付成功
     */
    override fun showOrderPaymentSuccess(data: JSONObject) {
        val paymentSuccess = FragmentOrderPayPinSuccess(this,data)
        replaceContainerFragement(R.id.rootContainer,paymentSuccess)
    }

    /**
     * 支付失败
     */
    override fun showOrderPaymentFailed(data: JSONObject) {
        val paymentFailedFm = FragmentOrderPayPinFailed(this,data)
        replaceContainerFragement(R.id.rootContainer,paymentFailedFm)
    }

    /**
     * 支付处理中
     */
    override fun showOrderPaymentProcess(data: JSONObject) {
        //是否是审核账号
        val isConfigAccount = ShipweSharedHelper.getShipweSharedBoolean(ShipweSharedKey.SHIPWE_IS_CONFIG_ACCOUNT)
        if(isConfigAccount) {//审核账号，直接进入还款失败
            showOrderPaymentFailed(data)
            return
        }
        val paymentProcessFm = FragmentOrderPayPinProcess(this,data)
        replaceContainerFragement(R.id.rootContainer,paymentProcessFm)
    }

    /**
     * 支付成功 - 跳转到可申请列表
     */
    fun showOrderPaySuccessAction(){
        //刷新产品列表
        publishEventData(AppEventData(AppEventData.TYPE.SHIPWE_PRODUCT_REFRESH))
        //返回首页
        publishEventData(AppEventData(AppEventData.TYPE.SHIPWE_SHOW_HOME))
        ShipweStackManager.finishAllActivity(ActivityMain::class.java)
    }
}